<?php
/**
 * 推荐商品的管理
 * Author: lyun
 * Date: 2018/4/12
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Wechat\RecommendProductV2IndexRequest;
use App\Models\Panx\Product;
use App\Models\Panx\ProductTag;
use App\Models\Store\SkinRecord;
use App\Models\Store\Store;
use App\Services\Repositories\Product\ProductRepository;
use App\Services\Repositories\Product\RecommendProductV2Repository;
use App\Services\Utils\PanxCollection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class RecommendProductV2Controller extends ApiController
{

    const ROUTES = [
        'index'       => 'wechat.recommend-product-v2.index',
        'index-group-by-category-id' => 'wechat.recommend-product-v2.index.group.by.category.id',
    ];

    /**
     * 根据肤质检测报告，推荐商品 (按照category_id分组)
     * @since v2.3.9
     * @test
     *
     * @param RecommendProductV2IndexRequest $request
     * @param RecommendProductV2Repository $recommendProdRepo
     * @return array|\Illuminate\Http\Response
     * @throws \App\Exceptions\ApiAbstractException
     */
    public function indexGroupByCategoryId(RecommendProductV2IndexRequest $request,
                                           RecommendProductV2Repository $recommendProdRepo)
    {
        $skin_record_id = (int)$request->skin_record_id;
        $skin_detector_id = (int)$request->skin_detector_id;
        $type_skin_record_id = (int)$request->type_skin_record_id;

        $skin_record = (new SkinRecord)->find($skin_record_id);
            $this->checkItemExist($skin_record, trans('wechat.skin_record'));
            $this->checkIsTrue($skin_record->skin_detector_id === $skin_detector_id, trans('wechat.skin_detector'));
            $this->checkIsTrue($skin_record->wechatUser->openid === $request->openid, trans('wechat.skin_record_wechat_user_not_match'));
            $this->checkIsTrue($skin_record->isHasWaxOrHmSkinRecords($type_skin_record_id), trans('wechat.skin_record_error'));

        $tag_ids = $recommendProdRepo->getTagIds($skin_detector_id, $type_skin_record_id);

        $body_position_id = $recommendProdRepo->getBodyPositionId($skin_detector_id, $type_skin_record_id);

        $category_id = $recommendProdRepo->getRecommendCategoryId($skin_detector_id, $type_skin_record_id);

        /** @var Store $store */
        $store = $skin_record->store;

        $recommend_products = $recommendProdRepo->recommendStoreProducts($store, $tag_ids, $request->sex, $body_position_id, $category_id);

        $recommend_products = $this->assembleProducts($recommend_products);

        // 根据category_id分组商品
        $group_recommend_products = $recommendProdRepo->transformRecommendProducts($recommend_products);

        return $this->response(compact('group_recommend_products'));
    }

    /**
     * @param Collection $recommend_products
     * @return Collection
     */
    private function assembleProducts(Collection $recommend_products)
    {
        (new PanxCollection($recommend_products))
            ->eachAppend(['tag_names', 'thumb_url', 'link_product', 'brand_name'])
            ->makeHidden(['thumbImage', 'productTags', 'brand']);

        return $recommend_products;
    }
}
